#import "SecurityViewController.h"


@implementation SecurityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self HexTest]; //16进制和非16进制字符串转换
    [self Base64Test]; //Base64编码
    [self HASHTest];  //MD5 SHA编码
    [self HMACSHATest]; //HMACSHA512编码
    [self AESTest]; // AES加解密
    [self DESTest];// DES加解密
    [self DESEDETest];// 3DES加解密
    [self RSATest]; // 标准RSA加解密
    [self LXRSATest]; //某公司加密
    
}

-(void)HexTest{
    NSString* source = @"123abada!@#$!$一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九123abada!@#$!$";
    NSString* encodeHexStr =[AllinHexUtil encode:[source dataUsingEncoding:NSUTF8StringEncoding]];
    NSString* decodeStr = [[NSString alloc]initWithData:[AllinHexUtil decode:encodeHexStr]encoding:NSUTF8StringEncoding];
    NSLog(@"%@",encodeHexStr);
    NSLog(@"%@",decodeStr);
}

-(void)Base64Test{
    NSString* source = @"123abada!@#$!$一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九123abada!@#$!$";
    NSData* data =[source dataUsingEncoding:NSUTF8StringEncoding];
    NSString* base64EncodeStr = [AllinBase64Util encode:data];
    NSString* decodeStr = [[NSString alloc]initWithData:[AllinBase64Util decode:base64EncodeStr] encoding:NSUTF8StringEncoding];
    NSLog(@"%@",base64EncodeStr);
    NSLog(@"%@",decodeStr);
}

-(void)HMACSHATest{
    NSString* pwd = @"pwd123aadf23sdfrdsvzxeRFASDFASDFASDZXCVASDFpwd123aadf23sdfrdsvzxeRFASDFASDFASDZXCVASDFpwd123aadf23sdfrdsvzxeRFASDFASDFASDZXCVASDFpwd123aadf23sdfrdsvzxeRFASDFASDFASDZXCVASDF";
    NSString* source = @"abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临";
    NSLog(@"%@",[AllinHMACSHAUtil encrypt:pwd content:source]);
}

- (void)HASHTest{
    NSString* source = @"123abada!@#$!$一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九123abada!@#$!$";
    
    NSLog(@"%@",[AllinMD5Util encrypt:source]);
    NSLog(@"%@",[AllinSHAUtil encryptSHA1:source]);
    NSLog(@"%@",[AllinSHAUtil encryptSHA256:source]);
    NSLog(@"%@",[AllinSHAUtil encryptSHA384:source]);
    NSLog(@"%@",[AllinSHAUtil encryptSHA512:source]);
    
}

-(void)RSATest{
    NSString* key = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtIKLlxTwWwy/N26JUgHjrgL6jH1kkqDUmZDcxH+v/v+9dJwxc4aCn6TLIYWaAF+8IPuHlX82rRm2isQyhbAOK6alkCnJu6zroeYqvBg9rr5kA9arA3qHaJ3sLAVD2E8uhyOJtpyMulF3OcZamKhlnfN10g8gls4goK9wvQvcBRwIDAQAB";
    NSString* privPKCS1Key = @"MIICXAIBAAKBgQCtIKLlxTwWwy/N26JUgHjrgL6jH1kkqDUmZDcxH+v/v+9dJwxc4aCn6TLIYWaAF+8IPuHlX82rRm2isQyhbAOK6alkCnJu6zroeYqvBg9rr5kA9arA3qHaJ3sLAV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g9xhgsafWHJLH9Vs83G4dwJ2xxPX88g9PTUGkrtCUXO4RMt3DBTC2e5GsXlE7BexCBiLJ5XMNeL+2jXH8UB93AkEAxQLVCKAuie9KEunY+wUDJbZ9AggZZQhhOi+D+szcrhzxJg2vUHUlLLQ6jc6zx0tkh2i8D1p4XaWCmK8UtcjAsQJAM0pi9njKyVhOZuSb3q5rNMFbstIzO2586xm0I7XvvEPLy4QLZ5FhX9mghuoiHxDkotD/UYTMzH+BRj/NgzdJVQJADNtrdb2ma+AO8H8MZw1jsDFhpSFMLvjGB5sUPOjjugUvg7E+8LxEac91Rs3nk6kK7X9bTpEz68Cibj521oo8cQJAZmBm3eU+VJxeT+7aTtJVKOI+QYN6hpUOrtJai5YVPQvqGs5P5dBtUAaFTADIEK50kayq9tybmnnlJQ8McpRPHQ==";
    NSString* targetText = @"dJ9LhgqayDA5tGel/Sp9/uwvjgt0aK1SWe8Tks4+Vxkyfvc+5G15DiUX7ikh7V9GhOBNqtJFXiPEX6Gr8O7SfCEuTEWMbl0/oQ4GM7xMiDH8ujjEIDvh3OwS7d6Z22l3N+hH7ZK4n0N8zhsY9YFDiW4Tgnlb9XqjDascgMxhMY0=";
    NSString* source = @"一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九";
    
    NSString* encodeBase64Str =[AllinRSAUtil encryptString:source publicKey:key];
    NSLog(@"%@",encodeBase64Str);
    
    NSLog(@"%@",[AllinRSAUtil decryptString:encodeBase64Str privateKey:privPKCS1Key]);
    NSLog(@"%@",[AllinRSAUtil decryptString:encodeBase64Str privateKey:privPKCS8Key padding:kSecPaddingPKCS1 isPKCS8:YES]);
    
    NSLog(@"From java encrypted-->%@",[AllinRSAUtil decryptString:targetText privateKey:privPKCS1Key]);
    
    //    NSLog(@"%@",[RSAUtil decry);
    
}

-(void)LXRSATest{
    //    NSString* key =  @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+JONy3BsyljOmoBSnwCeXkV48Nj3W7hGTd71kUym35kRrSnGsX4eOvIcMbYv9PAa5LmxukzkLNBkqep5DCAAJF2FHP6wswKaKgosliajLcpwZFh6OJ/TA883pcB+v5I0rZZR3mQcq+mo/LXvb74ttdCnC0RWj5fcjRn1Xs6xpZwIDAQAB";
    NSLog(@"%@",[AllinLXRSAUtil rsaEncrypt:@"欢迎pwd123我的博客"]);
}
NSString* origStr = @"abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临abcadflkjFC2016182l3kj!@!@#!@#欢迎光临我的博客abcadflkjFC2016182l3kj!@!@#!@#欢迎光临";

-(void)AESTest{
    //DES是64位 8字节编码key
    NSString* key = @"XGdiIQRn1283ui90";
    NSString* base64String  = [[AllinAESUtil AES128EncryptedDataWithKey:key data:[origStr  dataUsingEncoding:NSUTF8StringEncoding]] base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
    NSLog(@"%@",base64String);
    NSString* hexString  = [AllinHexUtil encode:[AllinAESUtil AES128EncryptedDataWithKey:key data:[origStr  dataUsingEncoding:NSUTF8StringEncoding]]];
    NSLog(@"%@",hexString);
    NSData* base64Data = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
    NSData* data =[AllinAESUtil AES128DecryptedDataWithKey:key data:base64Data];
    NSString* origMessage = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",origMessage);
}

-(void)DESTest{
    //DES是64位 8字节编码key
    NSString* key = @"XGdiIQRn";
    NSString* base64String  = [[AllinDESUtil DES64EncryptedDataWithKey:key data:[origStr  dataUsingEncoding:NSUTF8StringEncoding]] base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
    NSLog(@"%@",base64String);
    NSData* base64Data = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
    NSData* data =[AllinDESUtil DES64DecryptedDataWithKey:key data:base64Data];
    NSString* origMessage = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",origMessage);
}


-(void)DESEDETest{
    //DESEDE是192位 24字节编码key
    NSString* key = @"XGdiIQRnXROOX598XGdiIQRn";
    NSString* base64String  = [[AllinDESEDEUtil DESEDEEncryptedDataWithKey:key data:[origStr  dataUsingEncoding:NSUTF8StringEncoding]] base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
    NSLog(@"%@",base64String);
    NSData* base64Data = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
    NSData* data =[AllinDESEDEUtil DESEDEDecryptedDataWithKey:key data:base64Data];
    NSString* origMessage = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",origMessage);
}

@end
